package com.bdqn.tour.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 数据库的连接工具类
 */
public abstract class BaseDao {

    //创建预编译对象
    protected   Connection conn ;
    protected  PreparedStatement psmt ;
    protected ResultSet rs ;

    //创建C3P0的连接池对象
  /*  ComboPooledDataSource dataSource = new ComboPooledDataSource();*/

    /**
     * 获取数据库连接对象
     * @return 数据库的连接对象
     */
    public Connection getConnection(){
  /*      Connection connection = null;
        try {
            //从连接池中获取连接对象
            connection =   dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  connection;*/
        Connection connection = null;
        try {
            //创建配置文件对象
            Properties properties = new Properties();
            InputStream is=  BaseDao.class.getClassLoader().getResourceAsStream("database.properties");
            properties.load(is);
            //读取配置文件信息的key
            String driver = properties.getProperty("jdbc.driver");
            String url = properties.getProperty("jdbc.url");
            String user = properties.getProperty("jdbc.user");
            String password = properties.getProperty("jdbc.password");
            //加载驱动
            Class.forName(driver);
            //获取数据库连接对象
            connection = DriverManager.getConnection(url,user,password);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  connection;
    }


    /**
     * 执行查询
     * @param sql sql语句
     * @param params  传入的参数
     * @return 返回结果集对象
     * @throws Exception 抛出异常
     */
    public ResultSet executeQuery(String sql,Object... params) throws SQLException{

        this.psmt = conn.prepareStatement(sql);
        for(int i = 0 ; i < params.length ; i ++){
            System.out.println(i + "  " + params[i]);
            //设置参数
            this.psmt.setObject(i+1,params[i]);
        }
        //执行查询
        rs = psmt.executeQuery();
        return rs;
    }

    /**
     * 执行修改
     * @param sql 更新的SQL
     * @param params 传入的参数
     * @return 返回结果影响函数
     * @throws SQLException 抛出异常
     */
    public int executeUpdate(String sql,Object... params) throws SQLException{
        this.psmt = conn.prepareStatement(sql);
        for(int i = 0 ; i < params.length ; i ++){
            System.out.println(i + "  " + params[i]);
            this.psmt.setObject(i+1,params[i]);
        }
        int i = psmt.executeUpdate();
        return i;
    }

    /**
     * 释放资源
     * @param conn 数据库的连接对象
     * @param psmt 预编译对象
     * @param rs  结果集对象
     */
    public  void closeAll(Connection conn , PreparedStatement psmt , ResultSet rs){
        try {
            if(rs != null){
                rs.close();
            }

            if(psmt != null){
                psmt.close();
            }

            if(conn != null){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}